package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductTempBasic;
import com.qckj.amz.domain.ProductTempImport;
import com.qckj.amz.service.*;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.domain.entity.SysUser;
import com.qckj.common.core.domain.model.LoginUser;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.ServletUtils;
import com.qckj.common.utils.poi.ExcelUtil;
import com.qckj.framework.web.service.TokenService;
import com.qckj.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品基础信息Controller
 *
 * @author luchuan
 * @since 1.0 2021-12-20
 */
@RestController
@RequestMapping("/amz/productTempBasic")
public class ProductTempBasicController extends BaseController {
    @Autowired
    private IProductTempBasicService productTempBasicService;
    @Autowired
    private IProductTempRlatService productTempRlatService;
    @Autowired
    private IProductTempDescService productTempDescService;
    @Autowired
    private IProductTempAttrService productTempAttrService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IProductTempImportService productTempImportService;

    /**
     * 查询产品基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductTempBasic productTempBasic) {
        Page<ProductTempBasic> page = productTempBasicService.findProductTempPage(productTempBasic);
        return getDataTable(page);
    }

    /**
     * 获取产品基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productTempBasicService.findById(id));
    }

    /**
     * 修改产品基础信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:edit')")
    @Log(title = "产品基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductTempBasic productTempBasic) {
        productTempBasicService.save(productTempBasic);
        return AjaxResult.success(productTempBasic);
    }

    /**
     * 删除产品基础信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:remove')")
    @Log(title = "产品基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productCodes}")
    public AjaxResult remove(@PathVariable String[] productCodes) {
        List<String> productCodeList = Arrays.asList(productCodes);
        //通过productCode删除产品相关数据
        //暂时不处理，后期改用逻辑删除
        productTempBasicService.deleteByProductCodes(productCodeList);
        productTempRlatService.deleteByProductCodes(productCodeList);
        productTempDescService.deleteByProductCodes(productCodeList);
        productTempAttrService.deleteByProductCodes(productCodeList);
        return AjaxResult.success();
    }

    /**
     * 通过产品代码获取产品信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:query')")
    @RequestMapping(value = "/getListByProductCode")
    public AjaxResult getListByProductCode(@RequestBody String productCode) {
        AjaxResult res = productTempBasicService.getListByProductCode(productCode);
        return res;
    }

    /**
     * 新增产品基本信息和附加属性
     * @param formData
     *             var formData ={
     *               productTempBasic:this.form,
     *               productTempAttr:this.formAttr
     *             }
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:add')")
    @PostMapping("/addProductTempBasic")
    public AjaxResult addProductTempBasic(@RequestBody Map<String, Object> formData) {
        try {
            AjaxResult res = productTempBasicService.addProductTempBasic(formData);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改产品基本信息和附加属性
     * @param formData
     *             var formData ={
     *               productTempBasic:this.form,
     *               productTempAttr:this.formAttr
     *             }
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:edit')")
    @PostMapping("/updateProductTempBasic")
    public AjaxResult updateProductTempBasic(@RequestBody Map<String, Object> formData) {
        try {
            AjaxResult res = productTempBasicService.updateProductTempBasic(formData);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 导出产品
     * @param productCategory   产品分类
     * @param productCodes      产品代码
     * @param countryCodes      国家代码
     * @param productInitId    模板id(常用属性)
     * @param externalProductIdType    商品编码类型
     * @param generateExternalProductIdType    商品编码生成方式：1重新生成；0补齐生成（默认）
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:export')")
    @Log(title = "导出产品", businessType = BusinessType.EXPORT)
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public AjaxResult export(@RequestParam(name = "productCategory") String productCategory,
                             @RequestParam(name = "productCodes") List<String> productCodes,
                             @RequestParam(name = "countryCodes") List<String> countryCodes,
                             @RequestParam(name = "productInitId") String productInitId,
                             @RequestParam(name = "externalProductIdType") String externalProductIdType,
                             @RequestParam(name = "generateExternalProductIdType") String generateExternalProductIdType
    ) {

        AjaxResult res = productTempBasicService.exportProductTemp(productCategory,productCodes,countryCodes,productInitId,externalProductIdType,generateExternalProductIdType);
        return res;
    }

    /**
     * 执行认领产品
     * @param productCodes  产品代码
     * @param userName      用户名称
     * @param shopCode      店铺代码
     *                    shopCode当前参数暂时不适应，前端暂时隐藏不显示，
     *                    认领后登录人上传时可以自由选择绑定的店铺和站点，
     *                    后期如果设置高级权限时可以在认领时根据权限等级进行控制用户可上传的店铺
     *                    参数注释有效期至2023-12-31可删除
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:claim')")
    @Log(title = "认领产品", businessType = BusinessType.CLAIM)
    @RequestMapping(value = "/claimProductTempToFml", method = RequestMethod.POST)
    public AjaxResult claimProductTempToFml(
            @RequestParam(name = "productCodes") List<String> productCodes
            ,@RequestParam(name = "userName") String userName
            ,@RequestParam(name = "shopCode") String shopCode
    ) {

        AjaxResult res = productTempBasicService.claimProductTempToFml(productCodes,userName,shopCode);
        return res;
    }

    /**
     * 认领详情
     * @param productCode
     * @return
     */
    @RequestMapping(value = "/claimNumDetail", method = RequestMethod.POST)
    public AjaxResult claimNumDetail(@RequestParam(name = "productCode") String productCode) {

        AjaxResult res = productTempBasicService.claimNumDetail(productCode);
        return res;
    }

    /**
     * 产品翻译
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:translate')")
    @Log(title = "产品翻译", businessType = BusinessType.TRANSLATE)
    @GetMapping("/translate/{productCodes}")
    public AjaxResult translate(@PathVariable List<String> productCodes) {
        if (productCodes==null || productCodes.isEmpty()){
            return AjaxResult.error("产品代码不能为空！");
        }
        AjaxResult res = productTempBasicService.translate(productCodes);
        return res;
    }

    /**
     * 通过当前登录人获取当前用户及部门相关的用户，admin可以获取所有的正常用户信息
     * @return
     */
    @GetMapping("/getUserByLoginuser")
    public AjaxResult getUserByLoginuser() {
        AjaxResult res= productTempBasicService.getUserByLoginuser();
        return res;
    }

    /**
     * 校验产品完整性
     */
    @PostMapping("/handleCheckProductComplete")
    public AjaxResult handleCheckProductComplete(@RequestParam(name = "productCodes") List<String> productCodes) {
        AjaxResult res = productTempBasicService.handleCheckProductComplete(productCodes);
        return res;
    }

    /**
     * 暂存产品
     * @param productCodes
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempBasic:isStaging')")
    @RequestMapping(value = "/isStagingProductTempBasic", method = RequestMethod.POST)
    public AjaxResult isStagingProductTempBasic(@RequestParam(name = "productCodes") List<String> productCodes) {
        AjaxResult res = productTempBasicService.isStagingProductTempBasic(productCodes);
        return res;
    }

    @Log(title = "产品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ProductTempImport> util = new ExcelUtil<ProductTempImport>(ProductTempImport.class);
        List<ProductTempImport> productTempImportList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = productTempImportService.importProductTemp(productTempImportList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<ProductTempImport> util = new ExcelUtil<ProductTempImport>(ProductTempImport.class);
        return util.importTemplateExcel("产品数据");
    }

}
